<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>示例5</title>
</head>
<style type="text/css">
    *{
        text-align: center;
    }
</style>
<body>
    <div id="app" style="margin: 100px;">
     <parent></parent>
    </div>
    <template id="par">
        <div>
           <h1>{{parentMsg}}:{{message}}</h1>
           <child1 @change="getVal"></child1>
        </div>
    </template>
    <template id="c1">
        <div>
           <button @click="fn">点击传值父组件</button>
           <p>准备传递的数据:{{cMsg1}}</p>
        </div>
    </template>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <script type="text/javascript">
        var vm = new Vue({
            el: '#app',
            data: {

            },
        components: {
            "parent":{
                template:"#par",
                data(){
                    return{
                        parentMsg:"父组件接收的信息",
                        message:""
                    }
                },
                methods:{
                    getVal: function(val){
                        //val:代表传递过程的值
                        this.message=val;
                        console.log("接收的信息为:"+this.message);
                    }
                },
                components:{
                    "child1":{
                        template:"#c1",
                        data(){
                            return{
                                cMsg1:"1000"
                            }
                        },
                        methods:{
                          fn: function (){
                                //自定事件
                                this.$emit("change",this.cMsg1);
                            }
                        }
                    }
                }
            }   
       }
  });
    </script>
</body>
</html>